package com.scmagic.flutter_atauth

import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import io.flutter.plugin.common.StringCodec

class FlutterAtauthPlugin(private val registrar: Registrar, channel: MethodChannel): MethodCallHandler {

  private val atAuthHandler: ATAuthHandler = ATAuthHandler()
  private val basicMsgChannel: BasicMessageChannel<String> =
    BasicMessageChannel<String>(registrar.messenger(), "flutter_atauth_platform", StringCodec.INSTANCE)

  init {
    atAuthHandler.setRegister(registrar)
    atAuthHandler.setPlatform(basicMsgChannel)
  }

  companion object {
    @JvmStatic
    fun registerWith(registrar: Registrar) {
      val channel = MethodChannel(registrar.messenger(), "flutter_atauth")
      channel.setMethodCallHandler(FlutterAtauthPlugin(registrar, channel))
    }
  }

  override fun onMethodCall(call: MethodCall, result: Result) {
    if(call.method == ATAuthHandler.GetLoginToken) {
      atAuthHandler.getLoginToken(call, result)
      return
    }

    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    } else {
      result.notImplemented()
    }
  }
}
